package com.arox.springinaction.post;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

@Component("mailMessageConverter")
public class MailMessageConverter implements MessageConverter {

    public Object fromMessage(Message message) throws JMSException, MessageConversionException {
	MapMessage mapMessage = (MapMessage) message;
	Mail mail = new Mail();
	mail.setMailId(mapMessage.getString("mailId"));
	mail.setCountry(mapMessage.getString("country"));
	mail.setWeight(mapMessage.getDouble("weight"));
	return mail;
    }

    public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
	Mail mail = (Mail) object;
	MapMessage message = session.createMapMessage();
	message.setString("mailId", mail.getMailId());
	message.setString("country", mail.getCountry());
	message.setDouble("weight", mail.getWeight());
	return message;
    }

}
